<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

class NvModelObjek extends JModel
{

	var $_id = null;
	var $_data = null;

	function __construct(){
		parent::__construct();
/*
		$array = JRequest::getVar('cid', array(0), '', 'array');
		$this->setId((int)$array[0]);
*/
		global $mainframe;
		$limit = $mainframe->getUserStateFromRequest( 'limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest( 'limitstart', 'limitstart', 0, 'int' );

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);			
	}

	function setId(){
	/*
		$array = JRequest::getVar('cid', array(0), '', 'array');
		//$id_par = (int)$array[0];
		$this->_id		= (int)$array[0];
		echo $this->_id;
	*/
		$this->_data	= null;
		$this->_total 	= null;
	}

	function getData(){

		global $mainframe,$option;
		$search	= $mainframe->getUserStateFromRequest( $option.'search','search','','string' );
		$search	= JString::strtolower( $search );		

		if ($search) {
			$where = 'and a.name LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
		}
		$task = JRequest::getVar('task');
		
		$query_view = '
		select a.id_pariwisata, a.name,a.latitude,a.longitude,b.nama_kota,c.nama_propinsi, a.keterangan
		from #__nusaview a, #__propinsi c, #__kota b
		where a.propinsi_id = c.propinsi_id and a.kota_id = b.kota_id and b.propinsi_id = c.propinsi_id '
		.$where 
		;	
		
		//echo $query_view;
		//mengambil id_pariwisata data yang akan di edit
		$array = JRequest::getVar('cid', array(0), '', 'array');
		$id_par = (int)$array[0];
		$query_edit = ' SELECT a.*, propinsi.nama_propinsi AS nama_propinsi,'
			. ' kota.nama_kota AS nama_kota '
			. ' FROM #__nusaview AS a '
			. ' LEFT JOIN #__propinsi AS propinsi ON propinsi.propinsi_id = a.propinsi_id '
			. ' LEFT JOIN #__kota AS kota ON kota.kota_id = a.kota_id '		
			. ' where propinsi.propinsi_id = kota.propinsi_id and a.id_pariwisata ='.$id_par
			. $where
			;
		
		// ------------------ get data
		if (!isset($task) || empty($task) || !empty( $this->_data )) {
			$this->_data = $this->_getList( $query_view, $this->getState('limitstart'), $this->getState('limit') );	
			return $this->_data;		
		} 
		else {
			if ( empty($this->_data ) ){
				$this->_db->setQuery($query_edit);
				$this->_data = $this->_db->loadObject();			
			} 
			else {
				$this->_data = new stdClass();
				$this->_data->id = 0;
				$this->_data->name = null;					
			}				
			return $this->_data;					
		}
	}	

	function store($post){
		$row =& JTable::getInstance('nusaview', 'Table');
		$data = JRequest::get( 'post' );
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		$user =& JFactory::getUser();
		if($user->id){
			$row->user_id = $user->id;
		}
		// Store the web link table to the database
		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		return true;
	}
	
	function delete(){
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );	
		$row =& JTable::getInstance('nusaview', 'Table');

		if (count( $cids )){
			foreach($cids as $cid) {
				if (!$row->delete( $cid )) {
					$this->setError( $row->getErrorMsg() );
					return false;
				}
				$cid = intval( $cid );
				$query = 'DELETE FROM #__nusaview WHERE id_pariwisata='.$cid;
				
				$this->_db->setQuery( $this->_db->getEscaped( $query));
				if(!$this->_db->query()) {
					$this->setError($this->_db->getErrorMsg());
					return false;
				}			
			}				
		}
		return true;
	}
	
	function getTotal(){
		$query = "
		select a.id_pariwisata, a.name,a.latitude,a.longitude,b.nama_kota,c.nama_propinsi, a.keterangan
		from #__nusaview a, #__propinsi c, #__kota b
		where a.propinsi_id = c.propinsi_id and a.kota_id = b.kota_id and b.propinsi_id = c.propinsi_id
		";	
		if (empty($this->_total)){
			$this->_total = $this->_getListCount( $query );
		}
		return $this->_total;
	}
	
	function getPagination(){
		if (empty($this->_pagination)){
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}
		return $this->_pagination;
	}	
	
	
}
